/*
** my_path_to_wordtab.c for mysh in /home/nadali_t//local/svn/tek2ed/minishell1
** 
** Made by thomas nadalie
** Login   <nadali_t@epitech.net>
** 
** Started on  Thu Nov  8 12:23:16 2012 thomas nadalie
** Last update Thu Nov  8 12:24:33 2012 thomas nadalie
*/

#include	<stdlib.h>
#include	"my.h"

int		my_pathlen(char *str, int i)
{
  int		nb;

  nb = 0;
  while (str[i] && str[i] != ':')
    {
      i++;
      nb++;
    }
  return (nb);
}

int		count_path(char *str)
{
  int		i;
  int		nb;

  i = 0;
  nb = 0;
  while (str[i])
    {
      while (str[i] && str[i] == ':')
        i++;
      while (str[i] && str[i] != ':')
        i++;
      if (str[i])
	nb++;
    }
  return (nb);
}

char		*copy_path(char *str, int i)
{
  char		*new;
  int		j;

  j = 0;
  new = malloc(sizeof(*new) * my_pathlen(str, i) + 2);
  if (new == NULL)
    exit(1);
  while (str[i] && str[i] != ':')
    new[j++] = str[i++];
  new[j] = '/';
  j++;
  new[j] = '\0';
  return (new);
}

char		**my_path_to_wordtab(char *str)
{
  char		**tab;
  int		i;
  int		j;

  i = 0;
  j = 0;
  tab = malloc(sizeof(*tab) * ((count_path(str) + 2)));
  if (tab == NULL)
    return (NULL);
  while (str[i])
    {
      while (str[i] && str[i] == ':')
	i++;
      tab[j++] = copy_path(str, i);
      while (str[i] && str[i] != ':')
	i++;
    }
  tab[j] = NULL;
  return (tab);
}
